﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.IO;
using BrainTechLLC;
using System;
using BrainTechLLC.ThreadSafeObjects;

namespace ImageAppDataModels
{
    public class DirectoriesToExamineCollection : ObservableCollection<DirectoriesToExamine>
    {
        public static ICoreDataProvider DefaultDataProvider;
        public ICoreDataProvider _DataProvider;
        private object _lock = new object();

        public DirectoriesToExamineCollection() { }

        public ICoreDataProvider DataProvider
        {
            get
            {
                if (_DataProvider == null)
                {
                    _DataProvider = DefaultDataProvider;
                }
                return _DataProvider;
            }
            set
            {
                if (_DataProvider != value)
                {
                    _DataProvider = value;
                    // HEREHERE?
                }
            }
        }

        public void Save(string pathToExaminePath)
        {
            List<string> itemNames = new List<string>();

            List<DirectoriesToExamine> allItems = null;
            lock (_lock)
            {
                allItems = this.ToList();
            }
            
            foreach (var item in allItems)
            {
                item.Save(item.Name + ".dirs.dat");
                itemNames.Add(item.Name);
            }
            //foreach (var item in this)
            //{
            //    string fileName = item.Name + ".dirs.dat";
            //    item.Save(fileName);
            //    itemNames.Add(item.Name);
            //}
            byte[] bytes = itemNames.SerializeToCompressedBinary(true);
            DataProvider.WriteAllBytes(pathToExaminePath, bytes);
        }

        public static DirectoriesToExamineCollection Load(string _pathToExaminePath, ICoreDataProvider dataProvider)
        {
            DirectoriesToExamineCollection result = new DirectoriesToExamineCollection();

            if (dataProvider.Exists(_pathToExaminePath))
            {
                byte[] bytes = dataProvider.ReadAllImageBytes(_pathToExaminePath);
                List<string> itemNames = bytes.DeserializeFromCompressedBinary(true) as List<string>;

                if (itemNames != null)
                {
                    //var v = from item in itemNames.AsParallel()
                    //        let fileName = item + ".dirs.dat"
                    //        let initialPathsToExamine = ImageAppDataModels.DirectoriesToExamine.Load(fileName, dataProvider)
                    //        let pathsToExamine = initialPathsToExamine == null ? new string[] { Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) } : initialPathsToExamine
                    //        select new DirectoriesToExamine(pathsToExamine) { Name = item };

                    //v.ForAll(dirs => result.AddDir(dirs));

                    foreach (var item in itemNames)
                    {
                        string fileName = item + ".dirs.dat";
                        string[] pathsToExamine = ImageAppDataModels.DirectoriesToExamine.Load(fileName, dataProvider);

                        if (pathsToExamine == null)
                        {
                            pathsToExamine = new string[] { Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) };
                        }

                        DirectoriesToExamine dirs = new DirectoriesToExamine(pathsToExamine);
                        dirs.Name = item;
                        result.AddDir(dirs);
                    }
                }
                
            }

            return result;
        }

        private void AddDir(DirectoriesToExamine dirs)
        {
            lock (_lock)
            {
                Add(dirs);
            }
        }
    }

}
